<?php
/**
 * 邮件控制类API
 * @author tangbl
 *
 */
class EmailAction extends CommonAction {
	
	public function _initialize(){
		
		parent::_initialize();
	    //parent::checkTimestamp();
	}
	
	/**
	 * 群发邮件
	 * @param EmailSubscribe|Member $table 
	 */
	public function send(){
		
		$table  = $this->_req("table");
		$tempid = $this->_req("tempid");
		$from   = $this->_req("fromname");
		$name   = $this->_req("fromemail");
		$title  = $this->_req("title");
		$email  = $this->_req("email");
		$addid  = $this->_req("addid");
		$content= $this->_req("content");
		
		//$this->success(parent::input());
		$model  = D("Tip");
		if($tempid){
			$temp    = $model->getEmailTemplate($tempid);
			$content = $temp['content'];
			$title   = $temp['title'];
		}
		
		$content = htmlspecialchars_decode($content);
		$send    = 0;
		$succ    = 0;
		$fail    = 0;
		$illegal = "";
		$failure = "";
	
		$solevar = create_id();
		$array   = array("addid"=>$addid,"content"=>$content,"table"=>$table,"title"=>$title,
						 "solevar"=>$solevar,"tempid"=>$tempid,"email"=>$email);
		$newid   = $model->addEmailSend($array);
		
		$rows['solevar'] = $solevar;
		$rows['title']   = $title;
		$rows['content'] = $content;
		$rows['addid']   = $addid;
		
		$mail    = createMail();
		if($table){
		
			$moudle  = M("$table");
			$list    = $moudle->field("id,email")->select();
			foreach($list as $v){
				if(!isemail($v['email'])){
					$illegal .= $v['email']."|";
					continue;
				}
				$send++;
				$info = sendMail($mail,$v['email'],$title,$content,$name,$from,false);
				if($info[0]==1){
					$succ++;
				}else{
					$fail++;
					$failure .= $v['email']."|";
					$rows['status'] = 2;
					$rows['feed']   = $info[1];
				}
				$rows['email'] = $v['email'];
				$model->emailSendLog($rows);
			}
		}
		
		if($email){
			$lists   = explode(",", $email);
			foreach($lists as $v){
				if(!isemail($v)){
					$illegal .= $v."|";
					continue;
				}
				$send++;
				$info = sendMail($mail,$v,$title,$content,$name,$from,false);
				if($info[0]==1){
					$succ++;
				}else{
					$fail++;
					$failure .= $v."|";
					$rows['status'] = 2;
					$rows['feed']   = $info[1];
				}
				$rows['email'] = $v;
				$model->emailSendLog($rows);
			}
		}
		
		$row = array("id"=>$newid,"illegal"=>$illegal,"failure"=>$failure,"succ"=>$succ,"send"=>$send,"fail"=>$fail);
		$model->saveEmailSend($row);
		$this->success($row);
	}
	
	
	/**
	 * 发送邮件
	 */
	public function single(){
		
		$email  = $this->_get("email");
		$tempid = $this->_get("sign");
		$title  = $this->_get("title");
		$content= $this->_get("content");
		$lang   = $this->_get("lang");
		
		if(!isemail($email)){
			$this->error(L("em_email_error_errorTip"));
		}
		
		if($tempid){
			$temp    = $this->dealEmailTemp($tempid,$lang);
			$title   = $temp["title"];
			$content = $temp["content"];
		}
			
		$info = $this->email($email, $title, $content);
		
		if($info[0]==1){
			$this->success("",L("em_email_send_successTip"));
		}else{
			$this->error($info[1]);
		}
	}
	
	/**
	 * 通用处理邮件模板
	 */
	public function dealEmailTemp($sign,$lang){
	
		$model = D("Tip");
		
		$temp = $model->getEmailTemplate($sign,$lang);
		$this->assign("detail",$this->input());
		$temp['content'] = htmlspecialchars_decode($this->fetch("",stripslashes($temp['content'])));
		return $temp;
	}
	
	/**
	 * 发送邮件公共操作
	 */
	public function email($email,$title,$content){
	
		$mail = createMail();
		$info = sendMail($mail,$email,$title,$content);
	
		return $info;
	}
    
}